<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item label="券码" prop="couponCode">
        <el-input
          v-model="queryParams.couponCode"
          placeholder="请输入券码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="发放的用户" prop="nickName">
        <el-input
          v-model="queryParams.nickName"
          placeholder="请输入发放的用户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderSn">
        <el-input
          v-model="queryParams.orderSn"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="卡号" prop="couponCard">
        <el-input
          v-model="queryParams.couponCard"
          placeholder="请输入卡号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="对应的券名" prop="couponName">
        <el-input
          v-model="queryParams.couponName"
          placeholder="请输入对应的券名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="是否作废" prop="isDel">
        <el-select v-model="queryParams.isDel" placeholder="请选择">
          <el-option label="未作废" :value="1"></el-option>
          <el-option label="已作废" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="是否兑换" prop="isUse">
        <el-select v-model="queryParams.isUse" placeholder="请选择">
          <el-option label="未兑换" :value="1"></el-option>
          <el-option label="已兑换" :value="2"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
      </el-form-item>
    </el-form>
    <!-- <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-if="couponType != 3"
          v-hasPermi="['cancel:coupon:export']"
          >导出</el-button
        >
      </el-col>

      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row> -->
    <el-table v-loading="loading" :data="couponCodeList">
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="发放的用户" align="center" prop="nickName" />
      <el-table-column label="订单编号" align="center" prop="orderSn" />
      <el-table-column label="券码" align="center" prop="couponCode" />
      <el-table-column label="卡号" align="center" prop="couponCard" />
      <el-table-column label="对应的券名" align="center" prop="couponName" />
      <el-table-column label="券码金额" align="center" prop="couponPrice" />
      <el-table-column label="核销时间" align="center" prop="useTime" />
      <el-table-column label="券码状态" align="center">
        <template slot-scope="scope">
          <el-tag type="danger" v-if="scope.row.isDel == 2">已作废</el-tag>
          <el-tag type="success" v-else>正常</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="作废时间" align="center" prop="delTime" />

      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            v-if="scope.row.isDel == 1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cancel:couponCode:delCode']"
            >作废</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { mallCodeList, delCode } from "@/api/system/couponCode";

export default {
  name: "AllCode",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 券码列表表格数据
      couponCodeList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      couponType: "",
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询券码列表列表 */
    getList() {
      this.loading = true;
      mallCodeList(this.queryParams).then((response) => {
        this.couponCodeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    handleDelete(row) {
      const { id, couponName } = row;

      this.$modal
        .confirm("作废后将不可恢复，是否确认作废" + couponName + "？")
        .then(function () {
          return delCode({ id });
        })
        .then((res) => {
          this.getList();
          this.$modal.msgSuccess("此券已作废");
        })
        .catch(() => {});
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        dcNum: null,
      };
      this.resetForm("form");
    },

    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "cancel/coupon/export",
        {
          ...this.queryParams,
        },
        `coupon_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
